package blog.service.impl;

import blog.controller.vo.CommentDetailVo;
import blog.controller.vo.CommentPageResult;
import blog.entity.Comment;
import blog.entity.UserInfo;
import blog.mapper.CommentMapper;
import blog.service.CommentService;
import blog.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author StarryX
 * @since 2020-10-02
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public CommentPageResult createCommentPageResult(int blogId, int currentPage, int pageSize) {
        CommentPageResult commentPageResult = new CommentPageResult();
        List<CommentDetailVo> commentDetailVoList = new ArrayList<>();
        Page<Comment> page = new Page<>(currentPage, pageSize);
        commentService.page(page, new QueryWrapper<Comment>().eq("blog_id", blogId));

        for(Comment comment : page.getRecords()){
            CommentDetailVo commentDetailVo = createCommentDatailVo(comment);
            commentDetailVoList.add(commentDetailVo);
        }
        commentPageResult.setRecords(commentDetailVoList);
        commentPageResult.setCurrent(page.getCurrent());
        commentPageResult.setTotal(page.getTotal());
        return commentPageResult;
    }

    @Override
    public CommentDetailVo createCommentDatailVo(Comment comment) {
        CommentDetailVo commentDetailVo = new CommentDetailVo();
        UserInfo userInfo = userInfoService.getById(comment.getInfoId());
        commentDetailVo.setBlogId(comment.getBlogId());
        commentDetailVo.setContent(comment.getContent());
        commentDetailVo.setEmail(userInfo.getEmail());
        commentDetailVo.setNickName(userInfo.getNickName());
        commentDetailVo.setCreateTime(comment.getCreateTime());
        return commentDetailVo;
    }
}
